package com.macro.mall.demo.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.domain.EsIndexDto;
import com.macro.mall.demo.service.EsIndexService;
import io.swagger.annotations.Api;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author：贾维斯
 * @Description: 描述
 * @name：MatchQueryInfo
 * @Date：2024/10/22 9:42
 */
@RestController
@RequestMapping("/index")
@Validated
@Api(tags = "es索引管理")
public class EsIndexController {
    @Autowired
    private EsIndexService esIndexService;

    /**
     * 创建索引的接口
     * @param esIndexDto
     * @return
     */
    @PostMapping("/create")
    public CommonResult createIndex(@Validated @RequestBody EsIndexDto esIndexDto) {
         esIndexService.createIndex(esIndexDto);
        return CommonResult.success("索引创建成功"); // 调用服务方法创建索引
    }
    /**
     * 删除索引的接口
     * @param indexName
     * @return
     */
    @DeleteMapping("/delete")
    public CommonResult deleteIndex(@RequestParam @NotEmpty(message = "索引名称不能为空") String indexName) {
        esIndexService.deleteIndex(indexName); // 调用服务方法删除索引
        return CommonResult.success("索引删除成功");
    }

    /**
     *  获取索引的接口
     * @param indexName
     * @return
     */
    @GetMapping("/get")
    public CommonResult<Map<String, Object>> getIndex(@RequestParam  @NotEmpty(message = "索引名称不能为空") String indexName) {
        Map<String, Object> indexMappings = esIndexService.getIndex(indexName);
        return CommonResult.success(indexMappings); // 调用服务方法获取索引
    }


    /**
     * 根据索引名称修改索引配置
     * @param esIndexDto
     * @return
     */
    @PutMapping("/update")
    public CommonResult updateIndex(@Validated @RequestBody EsIndexDto esIndexDto) {
        esIndexService.updateIndex(esIndexDto);
        return CommonResult.success("索引更新成功"); // 调用服务方法更新索引
    }
    /**
     * 判断索引是否存在
     * @param indexName
     * @return
     */
    @GetMapping("/exists")
    public CommonResult exists(@RequestParam  @NotEmpty(message = "索引名称不能为空") String indexName) {
        boolean exists =esIndexService.exists(indexName);
        return CommonResult.success(exists);
    }
}
